<!DOCTYPE html>
<html  lang="zh-CN" >
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <meta name="theme-color" content="#fff" id="theme-color">
  <meta name="description" content="深山磨柴斧，一鸣震江湖">
  <link rel="icon" href="/img/ic_head.jpg">
  <title>025-Android自定义对话框（2）实践：版本更新Dialog</title>
  
  
  <meta property="og:title" content="025-Android自定义对话框（2）实践：版本更新Dialog">
  
  
  <meta property="og:url" content="http://example.com/2019/09/14/025-Android%E8%87%AA%E5%AE%9A%E4%B9%89%E5%AF%B9%E8%AF%9D%E6%A1%86%EF%BC%882%EF%BC%89%E5%AE%9E%E8%B7%B5%EF%BC%9A%E7%89%88%E6%9C%AC%E6%9B%B4%E6%96%B0Dialog/index.html">
  
  
  <meta property="og:img" content="/img/ic_head.jpg">
  
  
  <meta property="og:img" content="深山磨柴斧，一鸣震江湖">
  
  
  <meta property="og:type" content="article">
  <meta property="og:article:published_time" content="2019-09-14">
  <meta property="og:article:modified_time" content="2021-04-14">
  <meta property="og:article:author" content="Cupster">
  
  
  <meta property="og:article:tag" content="Android">
  
  <meta property="og:article:tag" content="Android Dialog">
  
  
  
  
  <script>
    // control reverse button
    var reverseDarkList = {
      dark: 'light',
      light: 'dark'
    };
    var themeColor = {
      dark: '#1c1c1e',
      light: '#fff'
    }
    // get the data of css prefers-color-scheme
    var getCssMediaQuery = function() {
      return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    };
    // reverse current darkmode setting function
    var reverseDarkModeSetting = function() {
      var setting = localStorage.getItem('user-color-scheme');
      if(reverseDarkList[setting]) {
        setting = reverseDarkList[setting];
      } else if(setting === null) {
        setting = reverseDarkList[getCssMediaQuery()];
      } else {
        return;
      }
      localStorage.setItem('user-color-scheme', setting);
      return setting;
    };
    // apply current darkmode setting
    var setDarkmode = function(mode) {
      var setting = mode || localStorage.getItem('user-color-scheme');
      if(setting === getCssMediaQuery()) {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[setting];
      } else if(reverseDarkList[setting]) {
        document.documentElement.setAttribute('data-user-color-scheme', setting);
        document.getElementById('theme-color').content = themeColor[setting];
      } else {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[getCssMediaQuery()];
      }
    };
    setDarkmode();
  </script>
  <script>
    function loadScript(url, cb) {
      var script = document.createElement('script');
      script.src = url;
      if (cb) script.onload = cb;
      script.async = true;
      document.body.appendChild(script);
    }
  </script>
  
  <link rel="preload" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css" as="style" >
  <link rel="preload" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css" as="style" >
  
  <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.js" as="script">
  <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.css" as="style" >
  
  
  <link rel="preload" href="/js/lib/lozad.min.js" as="script">
  
  
  
  
  
  
  
  
<link rel="stylesheet" href="/css/main.css">

  
  
<link rel="stylesheet" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css">

  
  
<link rel="stylesheet" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css">

  
  
<link rel="stylesheet" href="/js/lib/lightbox/baguetteBox.min.css">

  
  
  
  
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
  <div class="wrapper">
    
    <nav class="navbar">
  <div class="navbar-logo">
    <span class="navbar-logo-main">
      
      <img class="navbar-logo-img" src="/img/ic_head.jpg" alt="logo">
      
      <span class="navbar-logo-dsc">不负明天</span>
    </span>
  </div>
  <div class="navbar-menu">
    
    <a href="/" class="navbar-menu-item">
    
    首页
    
    </a>
    
    <a href="/archives" class="navbar-menu-item">
    
    归档
    
    </a>
    
    <a href="/tags" class="navbar-menu-item">
    
    标签
    
    </a>
    
    <a href="/categories" class="navbar-menu-item">
    
    分类
    
    </a>
    
    <a href="/about" class="navbar-menu-item">
    
    关于
    
    </a>
    
    <a href="/links" class="navbar-menu-item">
    
    友链
    
    </a>
    
    <a class="navbar-menu-item darknavbar" id="dark"><i class="iconfont icon-weather"></i></a>
    <a class="navbar-menu-item searchnavbar" id="search"><i class="iconfont icon-search" style="font-size: 1.2rem; font-weight: 400;"></i></a>
  </div>
</nav>
    
    <div id="local-search" style="display: none;">
      <input class="navbar-menu-item" id="search-input" placeholder="请输入搜索内容...">
      <div id="search-content"></div>
    </div>
    
    <div class="section-wrap">
      <div class="container">
        <div class="columns">
          <main class="main-column">
<article class="card card-content">
  <header>
    <h1 class="post-title">
      025-Android自定义对话框（2）实践：版本更新Dialog
    </h1>
  </header>
  <div class="post-meta post-show-meta">
    <time datetime="2019-09-14T04:43:32.000Z">
      <i class="iconfont icon-calendar" style="margin-right: 2px;"></i>
      <span>2019-09-14</span>
    </time>
    
    <span class="dot"></span>
    
    <a href="/categories/Android/" class="post-meta-link">Android</a>
    
    
    
    <span class="dot"></span>
    <span>2k 字</span>
    
  </div>
  
  <div class="post-meta post-show-meta" style="margin-top: -10px;">
    <div style="display: flex; align-items: center;">
      <i class="iconfont icon-biaoqian" style="margin-right: 2px; font-size: 1.15rem;"></i>
      
      
        <a href="/tags/Android/" class="post-meta-link">Android</a>
      
      
      <span class="dot"></span>
      
        <a href="/tags/Android-Dialog/" class="post-meta-link">Android Dialog</a>
      
    </div>
  </div>
  
  </header>
  <div id="section" class="post-content">
    <p><a target="_blank" rel="noopener" href="https://blog.csdn.net/Cupster/article/details/113727932">【5年Android从零复盘系列之二十五】Android自定义对话框（2）实践：版本更新Dialog</a></p>
<hr/>

<h1 id="1-简述"><a href="#1-简述" class="headerlink" title="1.简述"></a>1.简述</h1><p>如何自定义DialogFragment，请参考<a target="_blank" rel="noopener" href="https://blog.csdn.net/Cupster/article/details/113688377">Android自定义弹窗：DialogFragment</a></p>
<p>DialogFragment是谷歌官方推荐的对话框，可以高度自定义，灵活方便，非常适合编写产品经理设计的任何UI。</p>
<h1 id="2-需求"><a href="#2-需求" class="headerlink" title="2.需求"></a>2.需求</h1><p>设计自定义检查版本更新对话框，并加入动画效果，如下图：</p>
<img src="https://gitee.com/cupster/picbed/raw/master/uPic/20210206163610_j1fUEe.png" / srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://gitee.com/cupster/picbed/raw/master/uPic/20210206163610_j1fUEe.png" class="lozad post-image">

<img src="https:/ srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://gitee.com/cupster/picbed/raw/master/uPic/20210206163853_version.gif" class="lozad post-image"/gitee.com/cupster/picbed/raw/master/uPic/20210206163853_version.gif"/>

<h1 id="3-开发"><a href="#3-开发" class="headerlink" title="3.开发"></a>3.开发</h1><p><a target="_blank" rel="noopener" href="https://download.csdn.net/download/Cupster/15112384">代码.zip</a></p>
<h1 id="3-1-布局"><a href="#3-1-布局" class="headerlink" title="3.1 布局"></a>3.1 布局</h1><p>res/layout/xxx.xml</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">RelativeLayout</span> <span class="attr">xmlns:android</span>=<span class="string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:layout_width</span>=<span class="string">&quot;wrap_content&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:layout_height</span>=<span class="string">&quot;wrap_content&quot;</span></span></span><br><span class="line"><span class="tag"></span></span><br><span class="line"><span class="tag">    <span class="attr">android:background</span>=<span class="string">&quot;@android:color/transparent&quot;</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    &lt;LinearLayout</span><br><span class="line">        android:id=&quot;@+id/center_layout&quot;</span><br><span class="line">        android:layout_width=&quot;match_parent&quot;</span><br><span class="line">        android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">        android:orientation=&quot;vertical&quot;</span><br><span class="line">        android:background=&quot;@drawable/shape_white_radius_6_bg&quot;</span><br><span class="line">        android:layout_marginTop=&quot;65dp&quot;</span><br><span class="line">        &gt;</span><br><span class="line">        &lt;ImageView</span><br><span class="line">            android:layout_width=&quot;match_parent&quot;</span><br><span class="line">            android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">            android:src=&quot;@mipmap/pic_bo_lang&quot;</span><br><span class="line">            /&gt;</span><br><span class="line">        &lt;TextView</span><br><span class="line">            android:id=&quot;@+id/title&quot;</span><br><span class="line">            android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">            android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">            android:text=&quot;更新&quot;</span><br><span class="line">            android:textStyle=&quot;bold&quot;</span><br><span class="line">            android:textSize=&quot;16sp&quot;</span><br><span class="line">            android:textColor=&quot;#404040&quot;</span><br><span class="line"></span><br><span class="line">            android:layout_marginLeft=&quot;17dp&quot;</span><br><span class="line">            android:layout_marginRight=&quot;17dp&quot;</span><br><span class="line">            /&gt;</span><br><span class="line">        &lt;TextView</span><br><span class="line">            android:id=&quot;@+id/content&quot;</span><br><span class="line">            android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">            android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">            android:text=&quot;1.更新内容&quot;</span><br><span class="line">            android:textSize=&quot;14sp&quot;</span><br><span class="line">            android:textColor=&quot;#666666&quot;</span><br><span class="line">            android:layout_marginTop=&quot;8dp&quot;</span><br><span class="line">            android:layout_marginBottom=&quot;25dp&quot;</span><br><span class="line">            android:layout_marginLeft=&quot;17dp&quot;</span><br><span class="line">            android:layout_marginRight=&quot;17dp&quot;</span><br><span class="line">            /&gt;</span><br><span class="line"></span><br><span class="line">        &lt;LinearLayout</span><br><span class="line">            android:layout_width=&quot;match_parent&quot;</span><br><span class="line">            android:orientation=&quot;horizontal&quot;</span><br><span class="line">            android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">            android:layout_marginLeft=&quot;17dp&quot;</span><br><span class="line">            android:layout_marginRight=&quot;17dp&quot;</span><br><span class="line">            android:layout_marginBottom=&quot;25dp&quot;</span><br><span class="line">            &gt;</span><br><span class="line">            &lt;TextView</span><br><span class="line">                android:visibility=&quot;gone&quot;</span><br><span class="line">                android:id=&quot;@+id/btn_cancel&quot;</span><br><span class="line">                android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">                android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">                android:layout_weight=&quot;1&quot;</span><br><span class="line">                android:paddingTop=&quot;8dp&quot;</span><br><span class="line">                android:paddingBottom=&quot;8dp&quot;</span><br><span class="line">                android:textSize=&quot;16sp&quot;</span><br><span class="line">                android:layout_marginLeft=&quot;15dp&quot;</span><br><span class="line">                android:layout_marginRight=&quot;15dp&quot;</span><br><span class="line">                android:background=&quot;@drawable/shape_gray_efefef_radius_12&quot;</span><br><span class="line">                android:gravity=&quot;center&quot;</span><br><span class="line">                android:textColor=&quot;#666666&quot;</span><br><span class="line">                android:text=&quot;  取消  &quot;</span><br><span class="line">                /&gt;</span><br><span class="line">            &lt;TextView</span><br><span class="line">                android:id=&quot;@+id/btn_ok&quot;</span><br><span class="line">                android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">                android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">                android:layout_weight=&quot;1&quot;</span><br><span class="line">                android:paddingTop=&quot;8dp&quot;</span><br><span class="line">                android:paddingBottom=&quot;8dp&quot;</span><br><span class="line">                android:layout_marginLeft=&quot;15dp&quot;</span><br><span class="line">                android:layout_marginRight=&quot;15dp&quot;</span><br><span class="line">                android:textSize=&quot;16sp&quot;</span><br><span class="line">                android:background=&quot;@drawable/shape_btn_blue_radius_20&quot;</span><br><span class="line">                android:gravity=&quot;center&quot;</span><br><span class="line">                android:textColor=&quot;#fff&quot;</span><br><span class="line">                android:text=&quot;立即更新&quot;</span><br><span class="line">                /&gt;</span><br><span class="line"></span><br><span class="line">        <span class="tag">&lt;/<span class="name">LinearLayout</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;/<span class="name">LinearLayout</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    &lt;ImageView</span><br><span class="line">        android:layout_width=&quot;match_parent&quot;</span><br><span class="line">        android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">        android:src=&quot;@mipmap/ic_launch_rochet&quot;</span><br><span class="line">        android:maxHeight=&quot;65dp&quot;</span><br><span class="line">        android:layout_marginTop=&quot;10dp&quot;</span><br><span class="line">        /&gt;</span><br><span class="line">    &lt;View</span><br><span class="line">        android:id=&quot;@+id/un_use_line&quot;</span><br><span class="line">        android:layout_width=&quot;1dp&quot;</span><br><span class="line">        android:layout_height=&quot;45dp&quot;</span><br><span class="line">        android:background=&quot;#F0EFF2&quot;</span><br><span class="line">        android:layout_centerHorizontal=&quot;true&quot;</span><br><span class="line">        android:layout_below=&quot;@id/center_layout&quot;</span><br><span class="line">        /&gt;</span><br><span class="line">    &lt;ImageView</span><br><span class="line">        android:id=&quot;@+id/btn_close&quot;</span><br><span class="line">        android:layout_width=&quot;50dp&quot;</span><br><span class="line">        android:layout_height=&quot;50dp&quot;</span><br><span class="line">        android:paddingLeft=&quot;10dp&quot;</span><br><span class="line">        android:paddingRight=&quot;10dp&quot;</span><br><span class="line">        android:paddingTop=&quot;0dp&quot;</span><br><span class="line">        android:paddingBottom=&quot;20dp&quot;</span><br><span class="line">        android:src=&quot;@mipmap/ic_close_black&quot;</span><br><span class="line">        android:layout_centerHorizontal=&quot;true&quot;</span><br><span class="line">        android:layout_below=&quot;@id/un_use_line&quot;</span><br><span class="line">        /&gt;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">RelativeLayout</span>&gt;</span></span><br></pre>


<h2 id="3-2-类编写"><a href="#3-2-类编写" class="headerlink" title="3.2 类编写"></a>3.2 类编写</h2><p>其他次要代码见文末</p>
<pre class="highlight"><span class="line"><span class="keyword">package</span> com.cupster.versiondialog;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> android.content.Context;</span><br><span class="line"><span class="keyword">import</span> android.graphics.Color;</span><br><span class="line"><span class="keyword">import</span> android.graphics.drawable.ColorDrawable;</span><br><span class="line"><span class="keyword">import</span> android.os.Bundle;</span><br><span class="line"><span class="keyword">import</span> android.text.TextUtils;</span><br><span class="line"><span class="keyword">import</span> android.view.Gravity;</span><br><span class="line"><span class="keyword">import</span> android.view.LayoutInflater;</span><br><span class="line"><span class="keyword">import</span> android.view.View;</span><br><span class="line"><span class="keyword">import</span> android.view.View.OnClickListener;</span><br><span class="line"><span class="keyword">import</span> android.view.ViewGroup;</span><br><span class="line"><span class="keyword">import</span> android.view.Window;</span><br><span class="line"><span class="keyword">import</span> android.view.WindowManager;</span><br><span class="line"><span class="keyword">import</span> android.widget.ImageView;</span><br><span class="line"><span class="keyword">import</span> android.widget.TextView;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> androidx.annotation.NonNull;</span><br><span class="line"><span class="keyword">import</span> androidx.annotation.Nullable;</span><br><span class="line"><span class="keyword">import</span> androidx.fragment.app.DialogFragment;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">VersionOperateDialog</span> <span class="keyword">extends</span> <span class="title">DialogFragment</span> </span>&#123;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> ImageView mBtnClose;</span><br><span class="line">    <span class="keyword">private</span> TextView mTitle;</span><br><span class="line">    <span class="keyword">private</span> TextView mContent;</span><br><span class="line">    <span class="keyword">private</span> TextView mBtnOk;</span><br><span class="line">    <span class="keyword">private</span> TextView mBtnCancel;</span><br><span class="line">    <span class="keyword">private</span> View mLine ;</span><br><span class="line"></span><br><span class="line">    OnClickListener mPositiveListener ;</span><br><span class="line">    OnClickListener mNegativeListener;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> Object mVersionBean;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> VersionOperateDialog <span class="title">Build</span><span class="params">(String title,String content,<span class="keyword">boolean</span> closeShow)</span> </span>&#123;</span><br><span class="line">        VersionOperateDialog inputDialog = <span class="keyword">new</span> VersionOperateDialog();</span><br><span class="line">        Bundle bundle = <span class="keyword">new</span> Bundle();</span><br><span class="line">        bundle.putString(<span class="string">&quot;title&quot;</span> , title);</span><br><span class="line">        bundle.putString(<span class="string">&quot;content&quot;</span> , content);</span><br><span class="line">        bundle.putBoolean(<span class="string">&quot;closeShow&quot;</span> , closeShow);</span><br><span class="line">        inputDialog.setArguments(bundle);</span><br><span class="line">        <span class="keyword">return</span> inputDialog;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    View mView;</span><br><span class="line">    <span class="keyword">int</span> mWidth, mHeight;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Nullable</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> View <span class="title">onCreateView</span><span class="params">(<span class="meta">@NonNull</span> LayoutInflater inflater, <span class="meta">@Nullable</span> ViewGroup container, <span class="meta">@Nullable</span> Bundle savedInstanceState)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (mView == <span class="keyword">null</span>) &#123;</span><br><span class="line">            mView = inflater.inflate(R.layout.dialog_version_operate, container, <span class="keyword">false</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        setStyle(STYLE_NORMAL, R.style.InputDialogStyle);</span><br><span class="line">        getDialog().getWindow().setBackgroundDrawable(<span class="keyword">new</span> ColorDrawable(Color.TRANSPARENT));</span><br><span class="line">        <span class="keyword">return</span> mView;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onViewCreated</span><span class="params">(<span class="meta">@NonNull</span> View view, <span class="meta">@Nullable</span> Bundle savedInstanceState)</span> </span>&#123;</span><br><span class="line">        mTitle = mView.findViewById(R.id.title);</span><br><span class="line">        mContent = mView.findViewById(R.id.content);</span><br><span class="line">        mLine = mView.findViewById(R.id.un_use_line);</span><br><span class="line"></span><br><span class="line">        mBtnOk= mView.findViewById(R.id.btn_ok);</span><br><span class="line">        mBtnOk.setOnClickListener(<span class="keyword">new</span> OnClickListener() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onClick</span><span class="params">(View v)</span> </span>&#123;</span><br><span class="line">                <span class="keyword">if</span> (mPositiveListener != <span class="keyword">null</span>) &#123;</span><br><span class="line">                    mPositiveListener.onClick(v);</span><br><span class="line">                &#125;</span><br><span class="line">                dismiss();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        mBtnCancel= mView.findViewById(R.id.btn_cancel);</span><br><span class="line">        mBtnCancel.setOnClickListener(<span class="keyword">new</span> OnClickListener() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onClick</span><span class="params">(View v)</span> </span>&#123;</span><br><span class="line">                <span class="keyword">if</span> (mNegativeListener != <span class="keyword">null</span>) &#123;</span><br><span class="line">                    mNegativeListener.onClick(v);</span><br><span class="line">                &#125;</span><br><span class="line">                dismiss();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">        mBtnClose = mView.findViewById(R.id.btn_close);</span><br><span class="line">        mBtnClose.setOnClickListener(<span class="keyword">new</span> OnClickListener() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onClick</span><span class="params">(View v)</span> </span>&#123;</span><br><span class="line">                <span class="keyword">if</span> (mNegativeListener != <span class="keyword">null</span>) &#123;</span><br><span class="line">                    mNegativeListener.onClick(v);</span><br><span class="line">                &#125;</span><br><span class="line">                dismiss();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        initData();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">initData</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (getArguments() != <span class="keyword">null</span>)&#123;</span><br><span class="line">            <span class="keyword">boolean</span> closeShow = getArguments().getBoolean(<span class="string">&quot;closeShow&quot;</span>);</span><br><span class="line">            <span class="keyword">if</span> (!closeShow)&#123;</span><br><span class="line">                mLine.setVisibility(View.GONE);</span><br><span class="line">                mBtnCancel.setVisibility(View.GONE);</span><br><span class="line">                mBtnClose.setVisibility(View.GONE);</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            String title = getArguments().getString(<span class="string">&quot;title&quot;</span>);</span><br><span class="line">            <span class="keyword">if</span> (!TextUtils.isEmpty(title))&#123;</span><br><span class="line">                mTitle.setText(title);</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            String content = getArguments().getString(<span class="string">&quot;content&quot;</span>);</span><br><span class="line">            <span class="keyword">if</span> (!TextUtils.isEmpty(content))&#123;</span><br><span class="line">                mContent.setText(content);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onStart</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>.onStart();</span><br><span class="line">        <span class="comment">//在DialogFragment的onstart里面设置，可以将对话框外部的背景设为透明</span></span><br><span class="line"><span class="comment">//        Window window = getDialog().getWindow();</span></span><br><span class="line"><span class="comment">//        WindowManager.LayoutParams windowParams = window.getAttributes();</span></span><br><span class="line"><span class="comment">//        windowParams.dimAmount = 0.0f;</span></span><br><span class="line"><span class="comment">//        window.setAttributes(windowParams);</span></span><br><span class="line">        <span class="comment">//======================= ↑ 有效   ================================</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//======================= ↓ 有效   ================================</span></span><br><span class="line">        Window window = getDialog().getWindow();</span><br><span class="line">        window.setDimAmount(<span class="number">0.2f</span>);<span class="comment">//设置亮度</span></span><br><span class="line">        window.setGravity(Gravity.CENTER);</span><br><span class="line">        window.setWindowAnimations(R.style.CenterPopupAnimation);</span><br><span class="line">        <span class="comment">//全占宽、适配高</span></span><br><span class="line"><span class="comment">//        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);</span></span><br><span class="line">        <span class="comment">//自定义比例</span></span><br><span class="line">        WindowManager.LayoutParams layoutParams = window.getAttributes();</span><br><span class="line"><span class="comment">//        if (mWidth &gt; 0 &amp;&amp; mHeight &gt; 0) &#123;</span></span><br><span class="line"><span class="comment">//            layoutParams.height = mHeight;</span></span><br><span class="line"><span class="comment">//            layoutParams.width = mWidth;</span></span><br><span class="line"><span class="comment">//        &#125; else &#123;</span></span><br><span class="line"><span class="comment">//            layoutParams.height = deviceHeight(getContext()) ;</span></span><br><span class="line">        layoutParams.width = (<span class="keyword">int</span>) (<span class="number">0.7</span>*deviceWidth(getContext()) );</span><br><span class="line"><span class="comment">//        &#125;</span></span><br><span class="line">        window.setLayout(layoutParams.width, layoutParams.height);</span><br><span class="line">        window.setBackgroundDrawableResource(android.R.color.transparent);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span>  <span class="title">setObj</span><span class="params">(Object obj)</span></span>&#123;</span><br><span class="line">        mVersionBean = obj;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">getObj</span> <span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> mVersionBean;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setCancelListener</span><span class="params">(OnClickListener listener)</span> </span>&#123;</span><br><span class="line">        mNegativeListener = listener;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setOkListener</span><span class="params">(OnClickListener mPositiveListener)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.mPositiveListener = mPositiveListener;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 获取设备宽度（px）</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> context</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">deviceWidth</span><span class="params">(Context context)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> context.getResources().getDisplayMetrics().widthPixels;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 获取设备高度（px）</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> context</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">deviceHeight</span><span class="params">(Context context)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> context.getResources().getDisplayMetrics().heightPixels;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * dp 转化为 px</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> context</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> dpValue</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">dp2px</span><span class="params">(Context context, <span class="keyword">float</span> dpValue)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">final</span> <span class="keyword">float</span> scale = context.getResources().getDisplayMetrics().density;</span><br><span class="line">        <span class="keyword">return</span> (<span class="keyword">int</span>) (dpValue * scale + <span class="number">0.5f</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * px 转化为 dp</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> context</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> pxValue</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">px2dp</span><span class="params">(Context context, <span class="keyword">float</span> pxValue)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">final</span> <span class="keyword">float</span> scale = context.getResources().getDisplayMetrics().density;</span><br><span class="line">        <span class="keyword">return</span> (<span class="keyword">int</span>) (pxValue / scale + <span class="number">0.5f</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre>

<h2 id="3-3-使用"><a href="#3-3-使用" class="headerlink" title="3.3 使用"></a>3.3 使用</h2><pre class="highlight"><span class="line"><span class="keyword">package</span> com.cupster.versiondialog;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> androidx.appcompat.app.AppCompatActivity;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> android.os.Bundle;</span><br><span class="line"><span class="keyword">import</span> android.view.View;</span><br><span class="line"><span class="keyword">import</span> android.widget.Toast;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MainActivity</span> <span class="keyword">extends</span> <span class="title">AppCompatActivity</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">onCreate</span><span class="params">(Bundle savedInstanceState)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>.onCreate(savedInstanceState);</span><br><span class="line">        setContentView(R.layout.activity_main);</span><br><span class="line">        findViewById(R.id.btn).setOnClickListener(<span class="keyword">new</span> View.OnClickListener() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onClick</span><span class="params">(View view)</span> </span>&#123;</span><br><span class="line">                VersionOperateDialog dialog = VersionOperateDialog.Build(<span class="string">&quot;更新内容&quot;</span>, <span class="string">&quot;1.更新首页；\n2.更新导航布局；\n3.修复问题&quot;</span>, <span class="keyword">true</span>);</span><br><span class="line">                dialog.show(getSupportFragmentManager(), <span class="string">&quot;version&quot;</span>);</span><br><span class="line">                dialog.setOkListener(<span class="keyword">new</span> View.OnClickListener() &#123;</span><br><span class="line">                    <span class="meta">@Override</span></span><br><span class="line">                    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onClick</span><span class="params">(View v)</span> </span>&#123;</span><br><span class="line">                        <span class="comment">//下载安装包，更新APP</span></span><br><span class="line">                        Toast.makeText(MainActivity.<span class="keyword">this</span> ,<span class="string">&quot;下载中...&quot;</span>,Toast.LENGTH_SHORT).show();</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre>

<h1 id="3-4-动画"><a href="#3-4-动画" class="headerlink" title="3.4 动画"></a>3.4 动画</h1><p>res/anim/xxx.xml</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">set</span> <span class="attr">xmlns:android</span>=<span class="string">&quot;http://schemas.android.com/apk/res/android&quot;</span> &gt;</span></span><br><span class="line">    &lt;scale</span><br><span class="line">        android:duration=&quot;200&quot;</span><br><span class="line">        android:pivotX=&quot;50%p&quot;</span><br><span class="line">        android:pivotY=&quot;50%p&quot;</span><br><span class="line">        android:fromXScale=&quot;0.0&quot;</span><br><span class="line">        android:fromYScale=&quot;0.0&quot;</span><br><span class="line">        android:toXScale=&quot;1.2&quot;</span><br><span class="line">        android:toYScale=&quot;1.2&quot;/&gt;</span><br><span class="line"><span class="tag">&lt;/<span class="name">set</span>&gt;</span></span><br></pre>


<pre class="highlight"><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">set</span> <span class="attr">xmlns:android</span>=<span class="string">&quot;http://schemas.android.com/apk/res/android&quot;</span> &gt;</span></span><br><span class="line">    &lt;scale</span><br><span class="line">        android:duration=&quot;200&quot;</span><br><span class="line">        android:pivotX=&quot;0%p&quot;</span><br><span class="line">        android:pivotY=&quot;0%p&quot;</span><br><span class="line">        android:fromXScale=&quot;1.0&quot;</span><br><span class="line">        android:fromYScale=&quot;1.0&quot;</span><br><span class="line">        android:toXScale=&quot;0.0&quot;</span><br><span class="line">        android:toYScale=&quot;0.0&quot;/&gt;</span><br><span class="line"><span class="tag">&lt;/<span class="name">set</span>&gt;</span></span><br></pre>


<h2 id="3-5其他次要代码"><a href="#3-5其他次要代码" class="headerlink" title="3.5其他次要代码"></a>3.5其他次要代码</h2><p>1.shape_btn_blue_radius_20.xml</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">shape</span> <span class="attr">xmlns:android</span>=<span class="string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:shape</span>=<span class="string">&quot;rectangle&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">corners</span> <span class="attr">android:radius</span>=<span class="string">&quot;20dp&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">gradient</span> <span class="attr">android:startColor</span>=<span class="string">&quot;#508EF9&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:endColor</span>=<span class="string">&quot;#5577fb&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:angle</span>=<span class="string">&quot;135&quot;</span></span></span><br><span class="line"><span class="tag">        /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">shape</span>&gt;</span></span><br></pre>

<p>shape_gray_efefef_radius_12.xml</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">shape</span> <span class="attr">xmlns:android</span>=<span class="string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:shape</span>=<span class="string">&quot;rectangle&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">corners</span> <span class="attr">android:radius</span>=<span class="string">&quot;20dp&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">solid</span> <span class="attr">android:color</span>=<span class="string">&quot;#efefef&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">shape</span>&gt;</span></span><br></pre>

<p>shape_white_radius_6_bg.xml</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">shape</span> <span class="attr">xmlns:android</span>=<span class="string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:shape</span>=<span class="string">&quot;rectangle&quot;</span></span></span><br><span class="line"><span class="tag">    &gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">solid</span> <span class="attr">android:color</span>=<span class="string">&quot;#fff&quot;</span> /&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">corners</span> <span class="attr">android:radius</span>=<span class="string">&quot;10dp&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">shape</span>&gt;</span></span><br></pre>

<p>res/values/styles.xml 新增style</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">resources</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">style</span> <span class="attr">name</span>=<span class="string">&quot;InputDialogStyle&quot;</span> <span class="attr">parent</span>=<span class="string">&quot;android:Theme.Holo.Light.Dialog&quot;</span>&gt;</span></span><br><span class="line"><span class="xml">        <span class="tag">&lt;<span class="name">item</span> <span class="attr">name</span>=<span class="string">&quot;android:windowNoTitle&quot;</span>&gt;</span>true<span class="tag">&lt;/<span class="name">item</span>&gt;</span></span></span><br><span class="line"><span class="xml">        <span class="tag">&lt;<span class="name">item</span> <span class="attr">name</span>=<span class="string">&quot;android:windowBackground&quot;</span>&gt;</span>@null<span class="tag">&lt;/<span class="name">item</span>&gt;</span></span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 弹出窗体的动画 从中间浮出弹出 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">style</span> <span class="attr">name</span>=<span class="string">&quot;CenterPopupAnimation&quot;</span> <span class="attr">parent</span>=<span class="string">&quot;@android:style/Animation&quot;</span>&gt;</span></span><br><span class="line"><span class="xml">        <span class="comment">&lt;!-- 边框 --&gt;</span></span></span><br><span class="line"><span class="xml">        <span class="tag">&lt;<span class="name">item</span> <span class="attr">name</span>=<span class="string">&quot;android:windowFrame&quot;</span>&gt;</span>@null<span class="tag">&lt;/<span class="name">item</span>&gt;</span></span></span><br><span class="line"><span class="xml">        <span class="comment">&lt;!-- 设置dialog的背景 --&gt;</span></span></span><br><span class="line"><span class="xml">        <span class="tag">&lt;<span class="name">item</span> <span class="attr">name</span>=<span class="string">&quot;android:windowBackground&quot;</span>&gt;</span>@android:color/transparent<span class="tag">&lt;/<span class="name">item</span>&gt;</span></span></span><br><span class="line"><span class="xml">        <span class="comment">&lt;!-- 是否浮现在activity之上 --&gt;</span></span></span><br><span class="line"><span class="xml">        <span class="tag">&lt;<span class="name">item</span> <span class="attr">name</span>=<span class="string">&quot;android:windowIsFloating&quot;</span>&gt;</span>true<span class="tag">&lt;/<span class="name">item</span>&gt;</span></span></span><br><span class="line"><span class="xml">        <span class="comment">&lt;!-- 半透明 --&gt;</span></span></span><br><span class="line"><span class="xml">        <span class="tag">&lt;<span class="name">item</span> <span class="attr">name</span>=<span class="string">&quot;android:windowIsTranslucent&quot;</span>&gt;</span>false<span class="tag">&lt;/<span class="name">item</span>&gt;</span></span></span><br><span class="line"><span class="xml">        <span class="comment">&lt;!-- 无标题 --&gt;</span></span></span><br><span class="line"><span class="xml">        <span class="tag">&lt;<span class="name">item</span> <span class="attr">name</span>=<span class="string">&quot;android:windowNoTitle&quot;</span>&gt;</span>true<span class="tag">&lt;/<span class="name">item</span>&gt;</span></span></span><br><span class="line"><span class="xml">        <span class="comment">&lt;!-- 模糊 --&gt;</span></span></span><br><span class="line"><span class="xml">        <span class="tag">&lt;<span class="name">item</span> <span class="attr">name</span>=<span class="string">&quot;android:backgroundDimEnabled&quot;</span>&gt;</span>true<span class="tag">&lt;/<span class="name">item</span>&gt;</span></span></span><br><span class="line"><span class="xml">        <span class="tag">&lt;<span class="name">item</span> <span class="attr">name</span>=<span class="string">&quot;android:windowEnterAnimation&quot;</span>&gt;</span>@anim/popup_alpha_in<span class="tag">&lt;/<span class="name">item</span>&gt;</span></span></span><br><span class="line"><span class="xml">        <span class="tag">&lt;<span class="name">item</span> <span class="attr">name</span>=<span class="string">&quot;android:windowExitAnimation&quot;</span>&gt;</span>@anim/popup_alpha_out<span class="tag">&lt;/<span class="name">item</span>&gt;</span></span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">resources</span>&gt;</span></span><br></pre>


<h1 id="4-附"><a href="#4-附" class="headerlink" title="4.附"></a>4.附</h1><p><a target="_blank" rel="noopener" href="https://download.csdn.net/download/Cupster/15112384">代码.zip</a></p>

  </div>
  <div>
  
  <div class="post-note note-warning copyright" style="margin-top: 42px">
    <p><span style="font-weight: bold;">作者：</span><a target="_blank" rel="nofollow noopener noreferrer" href="http://example.com/about">Cupster</a></p>
    <p><span style="font-weight: bold;">文章链接：</span><a target="_blank" rel="nofollow noopener noreferrer" href="http://example.com/2019/09/14/025-Android%E8%87%AA%E5%AE%9A%E4%B9%89%E5%AF%B9%E8%AF%9D%E6%A1%86%EF%BC%882%EF%BC%89%E5%AE%9E%E8%B7%B5%EF%BC%9A%E7%89%88%E6%9C%AC%E6%9B%B4%E6%96%B0Dialog/">http://example.com/2019/09/14/025-Android%E8%87%AA%E5%AE%9A%E4%B9%89%E5%AF%B9%E8%AF%9D%E6%A1%86%EF%BC%882%EF%BC%89%E5%AE%9E%E8%B7%B5%EF%BC%9A%E7%89%88%E6%9C%AC%E6%9B%B4%E6%96%B0Dialog/</a></p>
    <p><span style="font-weight: bold;">版权声明：</span>本博客所有文章除特别声明外，均采用<a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">CC BY-NC-SA 4.0 协议</a>。转载请注明出处！</p>
  </div>
  
  </div>
</article>
<div class="nav">
  
  <div class="nav-item-prev">
    <a href="/2019/09/19/026-Android存储（1）：Sharedpreference详解/" class="nav-link">
      <i class="iconfont icon-left nav-prev-icon"></i>
      <div>
        <div class="nav-label">上一篇</div>
        
        <div class="nav-title">026-Android存储（1）：Sharedpreference详解 </div>
        
      </div>
    </a>
  </div>
  
  
  <div class="nav-item-next">
    <a href="/2019/09/09/024-Android自定义对话框（1）：DialogFragment图文详解/" class="nav-link">
      <div>
        <div class="nav-label">下一篇</div>
        
        <div class="nav-title">024-Android自定义对话框（1）：DialogFragment图文详解 </div>
        
      </div>
      <i class="iconfont icon-right nav-next-icon"></i>
    </a>
  </div>
  
</div>

<div class="card card-content toc-card" id="mobiletoc">
  <div class="toc-header"><i class="iconfont icon-menu" style="padding-right: 2px;"></i>目录</div>
<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-%E7%AE%80%E8%BF%B0"><span class="toc-text">1.简述</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-%E9%9C%80%E6%B1%82"><span class="toc-text">2.需求</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-%E5%BC%80%E5%8F%91"><span class="toc-text">3.开发</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-1-%E5%B8%83%E5%B1%80"><span class="toc-text">3.1 布局</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#3-2-%E7%B1%BB%E7%BC%96%E5%86%99"><span class="toc-text">3.2 类编写</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-3-%E4%BD%BF%E7%94%A8"><span class="toc-text">3.3 使用</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-4-%E5%8A%A8%E7%94%BB"><span class="toc-text">3.4 动画</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#3-5%E5%85%B6%E4%BB%96%E6%AC%A1%E8%A6%81%E4%BB%A3%E7%A0%81"><span class="toc-text">3.5其他次要代码</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-%E9%99%84"><span class="toc-text">4.附</span></a></li></ol>
</div></main>
          <aside class="left-column">
            
            <div class="card card-author">
              
<img src="/img/ic_head.jpg" class="author-img">

<p class="author-name">Cupster</p>
<p class="author-description">深山磨柴斧，一鸣震江湖</p>
<div class="author-message">
  <a class="author-posts-count" href="/archives">
    <span>66</span>
    <span>文章</span>
  </a>
  <a class="author-categories-count" href="/categories">
    <span>5</span>
    <span>分类</span>
  </a>
  <a class="author-tags-count" href="/tags">
    <span>31</span>
    <span>标签</span>
  </a>
</div>

<div class="author-card-society">
  
    <div class="author-card-society-icon">
      <a target="_blank" rel="noopener" href="https://cupster.blog.csdn.net/">
        <i class="iconfont /img/ic_csdn.png society-icon"></i>
      </a>
    </div>
  
</div>

            </div>
            
            <div class="sticky-tablet">
  
  
  <article class="display-when-two-columns spacer">
    <div class="card card-content toc-card">
      <div class="toc-header"><i class="iconfont icon-menu" style="padding-right: 2px;"></i>目录</div>
<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-%E7%AE%80%E8%BF%B0"><span class="toc-text">1.简述</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-%E9%9C%80%E6%B1%82"><span class="toc-text">2.需求</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-%E5%BC%80%E5%8F%91"><span class="toc-text">3.开发</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-1-%E5%B8%83%E5%B1%80"><span class="toc-text">3.1 布局</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#3-2-%E7%B1%BB%E7%BC%96%E5%86%99"><span class="toc-text">3.2 类编写</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-3-%E4%BD%BF%E7%94%A8"><span class="toc-text">3.3 使用</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-4-%E5%8A%A8%E7%94%BB"><span class="toc-text">3.4 动画</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#3-5%E5%85%B6%E4%BB%96%E6%AC%A1%E8%A6%81%E4%BB%A3%E7%A0%81"><span class="toc-text">3.5其他次要代码</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-%E9%99%84"><span class="toc-text">4.附</span></a></li></ol>
    </div>
  </article>
  
  
  <article class="card card-content categories-widget">
    <div class="categories-card">
  <div class="categories-header"><i class="iconfont icon-fenlei" style="padding-right: 2px;"></i>分类</div>
  <div class="categories-list">
    
      <a href="/categories/Android/">
        <div class="categories-list-item">
          Android
          <span class="categories-list-item-badge">38</span>
        </div>
      </a>
    
      <a href="/categories/Linux/">
        <div class="categories-list-item">
          Linux
          <span class="categories-list-item-badge">6</span>
        </div>
      </a>
    
      <a href="/categories/Dev-Base/">
        <div class="categories-list-item">
          Dev-Base
          <span class="categories-list-item-badge">12</span>
        </div>
      </a>
    
      <a href="/categories/Kotlin/">
        <div class="categories-list-item">
          Kotlin
          <span class="categories-list-item-badge">9</span>
        </div>
      </a>
    
      <a href="/categories/自动化脚本/">
        <div class="categories-list-item">
          自动化脚本
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
  </div>
</div>
  </article>
  
  <article class="card card-content tags-widget">
    <div class="tags-card">
  <div class="tags-header"><i class="iconfont icon-biaoqian" style="padding-right: 2px;"></i>热门标签</div>
  <div class="tags-list">
    
    <a href="/tags/Android/" title="Android"><div class="tags-list-item">Android</div></a>
    
    <a href="/tags/Android自定义View/" title="Android自定义View"><div class="tags-list-item">Android自定义View</div></a>
    
    <a href="/tags/MacOS/" title="MacOS"><div class="tags-list-item">MacOS</div></a>
    
    <a href="/tags/Kotlin/" title="Kotlin"><div class="tags-list-item">Kotlin</div></a>
    
    <a href="/tags/Android持久化/" title="Android持久化"><div class="tags-list-item">Android持久化</div></a>
    
    <a href="/tags/CentOS/" title="CentOS"><div class="tags-list-item">CentOS</div></a>
    
    <a href="/tags/Linux/" title="Linux"><div class="tags-list-item">Linux</div></a>
    
    <a href="/tags/Android事件/" title="Android事件"><div class="tags-list-item">Android事件</div></a>
    
    <a href="/tags/Android基础/" title="Android基础"><div class="tags-list-item">Android基础</div></a>
    
    <a href="/tags/Android手势/" title="Android手势"><div class="tags-list-item">Android手势</div></a>
    
    <a href="/tags/Scrcpy/" title="Scrcpy"><div class="tags-list-item">Scrcpy</div></a>
    
    <a href="/tags/Android动画/" title="Android动画"><div class="tags-list-item">Android动画</div></a>
    
    <a href="/tags/Git/" title="Git"><div class="tags-list-item">Git</div></a>
    
    <a href="/tags/Java/" title="Java"><div class="tags-list-item">Java</div></a>
    
    <a href="/tags/Docker/" title="Docker"><div class="tags-list-item">Docker</div></a>
    
    <a href="/tags/Android-Dialog/" title="Android Dialog"><div class="tags-list-item">Android Dialog</div></a>
    
  </div>
</div>
  </article>
  
  
</div>
          </aside>
          <aside class="right-column">
            <div class="sticky-widescreen">
  
  
  <article class="card card-content toc-card">
    <div class="toc-header"><i class="iconfont icon-menu" style="padding-right: 2px;"></i>目录</div>
<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-%E7%AE%80%E8%BF%B0"><span class="toc-text">1.简述</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-%E9%9C%80%E6%B1%82"><span class="toc-text">2.需求</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-%E5%BC%80%E5%8F%91"><span class="toc-text">3.开发</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-1-%E5%B8%83%E5%B1%80"><span class="toc-text">3.1 布局</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#3-2-%E7%B1%BB%E7%BC%96%E5%86%99"><span class="toc-text">3.2 类编写</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-3-%E4%BD%BF%E7%94%A8"><span class="toc-text">3.3 使用</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-4-%E5%8A%A8%E7%94%BB"><span class="toc-text">3.4 动画</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#3-5%E5%85%B6%E4%BB%96%E6%AC%A1%E8%A6%81%E4%BB%A3%E7%A0%81"><span class="toc-text">3.5其他次要代码</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-%E9%99%84"><span class="toc-text">4.附</span></a></li></ol>
  </article>
  
  
  <article class="card card-content">
    <div class="recent-posts-card">
  <div class="recent-posts-header"><i class="iconfont icon-wenzhang_huaban" style="padding-right: 2px;"></i>最近文章</div>
  <div class="recent-posts-list">
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-03-02</div>
        <a href="/2021/03/02/Git commit规范/"><div class="recent-posts-item-content">Git commit规范</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-02-16</div>
        <a href="/2021/02/16/AutoJS一文精通AutoJS脚本教程详解/"><div class="recent-posts-item-content">AutoJS一文精通AutoJS脚本教程详解</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-01-21</div>
        <a href="/2021/01/21/架构师进阶：注解Annotation全解/"><div class="recent-posts-item-content">架构师进阶：注解Annotation全解</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-01-06</div>
        <a href="/2021/01/06/ConstraintLayout详解/"><div class="recent-posts-item-content">ConstraintLayout详解</div></a>
      </div>
    
  </div>
</div>
  </article>
  
  
</div>
          </aside>
        </div>
      </div>
    </div>
  </div>
  
  <footer class="footer">
  <div class="footer-container">
    <div>
      <div class="footer-dsc">
        <span>Copyright ©
          
          2020 -
          
          2021
        </span>
        &nbsp;
        <a href="/" class="footer-link">不负明天 </a>
      </div>
    </div>

    
    <div class="footer-dsc">
      
      Powered by
      <a href="https://hexo.io/" class="footer-link" target="_blank" rel="nofollow noopener noreferrer">&nbsp;Hexo </a>
      
      
      <span>&nbsp;|&nbsp;</span>
      
      
      Theme -
      <a href="https://github.com/theme-kaze" class="footer-link" target="_blank"
        rel="nofollow noopener noreferrer">&nbsp;Kaze</a>
      
    </div>
    
    
    
    
      <div class="footer-dsc">
        
        本站总访问量686<span id="busuanzi_value_site_pv"></span>次
        
        
        <span>&nbsp;|&nbsp;</span>
        
        
        本站总访客数219<span id="busuanzi_value_site_uv"></span>次
        
      </div>
      
    
</footer>
  <a role="button" id="scrollbutton" class="basebutton"  aria-label="回到顶部">
  <i class="iconfont icon-arrowleft button-icon"></i>
</a>
<a role="button" id="menubutton" class="basebutton">
  <i class="iconfont icon-menu button-icon"></i>
</a>
<a role="button" id="popbutton" class="basebutton" aria-label="控制中心">
  <i class="iconfont icon-expand button-icon"></i>
</a>
<a role="button" id="darkbutton" class="basebutton darkwidget" aria-label="夜色模式">
  <i class="iconfont icon-weather button-icon"></i>
</a>
<a role="button" id="searchbutton" class="basebutton searchwidget" aria-label="搜索">
  <i class="iconfont icon-search button-icon"></i>
</a>

  
  
  
  <script>
  var addImgLayout = function () {
    var img = document.querySelectorAll('.post-content img');
    var i;
    for (i = 0; i < img.length; i++) {
      var wrapper = document.createElement('a');
      wrapper.setAttribute('href', img[i].getAttribute('data-src'));
      wrapper.setAttribute('aria-label', 'illustration');
      wrapper.style.cssText = 'width: 100%; display: flex; justify-content: center;';
      if (img[i].alt) wrapper.dataset.caption = img[i].alt;
      wrapper.dataset.nolink = true;
      img[i].before(wrapper);
      wrapper.append(img[i]);
      var divWrap = document.createElement('div');
      divWrap.classList.add('gallery');
      wrapper.before(divWrap);
      divWrap.append(wrapper);
    }
    baguetteBox.run('.gallery');
  }
</script>
<script>loadScript("/js/lib/lightbox/baguetteBox.min.js", addImgLayout)</script>
  
  
  
<script src="/js/main.js"></script>

  
  <script>loadScript("/js/lib/busuanzi.min.js")</script>
  
  
  <script>
    var addLazyload = function () {
      var observer = lozad('.lozad', {
        load: function (el) {
          el.srcset = el.getAttribute('data-src');
        },
        loaded: function (el) {
          el.classList.add('loaded');
        }
      });
      observer.observe();
    }
  </script>
  <script>loadScript("/js/lib/lozad.min.js", addLazyload)</script>
  
  
</body>

</html>